package can.bishe.modules.exam.service.impl;

import can.bishe.modules.sys.dao.SysDeptDao;
import can.bishe.modules.sys.entity.SysUserEntity;
import can.bishe.modules.sys.entity.SysUserRoleEntity;
import can.bishe.modules.sys.service.SysDeptService;
import can.bishe.modules.sys.service.SysUserRoleService;
import can.bishe.modules.sys.service.SysUserService;
import com.qiniu.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import can.bishe.common.utils.PageUtils;
import can.bishe.common.utils.Query;

import can.bishe.modules.exam.dao.ExamScoreDao;
import can.bishe.modules.exam.entity.ExamScoreEntity;
import can.bishe.modules.exam.service.ExamScoreService;

import javax.annotation.Resource;


@Service("examScoreService")
public class ExamScoreServiceImpl extends ServiceImpl<ExamScoreDao, ExamScoreEntity> implements ExamScoreService {

    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private SysDeptService sysDeptService;
    @Resource
    private SysDeptDao sysDeptDao;
    @Autowired
    private SysUserRoleService sysUserRoleService;

    @Override
    public PageUtils queryPage(Map<String, Object> params, SysUserEntity user) {
        String usernameQ = (String) params.get("username");
        String deptNameQ = (String) params.get("deptName");
        List<Long> userIdList = new ArrayList<>();
        System.out.println("参数" + usernameQ + deptNameQ);
        Long roleId = sysUserRoleService.getOne(new QueryWrapper<SysUserRoleEntity>().eq("user_id",user.getUserId())).getRoleId();
        Long deptIdOne = user.getDeptId();
        if(roleId.equals(4L)){
            deptNameQ = sysDeptService.getById(deptIdOne).getName();
            if(!StringUtils.isNullOrEmpty(usernameQ)){
                QueryWrapper qwu = new QueryWrapper();
                qwu.like("realname",usernameQ);
                List<SysUserEntity> userEntityList = sysUserService.list(qwu);
                for(SysUserEntity users:userEntityList){
                    userIdList.add(users.getUserId());
                }
                deptNameQ = null;
            }
            if(!StringUtils.isNullOrEmpty(deptNameQ)){
                QueryWrapper qwd = new QueryWrapper();
                qwd.eq("name",deptNameQ);
                Long deptId = sysDeptDao.selectOne(qwd).getDeptId();
                QueryWrapper qwus = new QueryWrapper();
                qwus.eq("dept_id",deptId);
                List<SysUserEntity> userEntityLists = sysUserService.list(qwus);
                for(SysUserEntity users:userEntityLists){
                    userIdList.add(users.getUserId());
                }
            }
            if(userIdList.size() != 0 ){
                IPage<ExamScoreEntity> page = this.page(
                        new Query<ExamScoreEntity>().getPage(params),
                        new QueryWrapper<ExamScoreEntity>()
                                .in("user_id",userIdList)
                );
                for(ExamScoreEntity examScoreOne:page.getRecords()){
                    SysUserEntity userOne = sysUserService.getById(examScoreOne.getUserId());
                    String realName = userOne.getRealname();
                    String deptName = sysDeptService.getById((userOne.getDeptId())).getName();
                    examScoreOne.setRealName(realName);
                    examScoreOne.setDeptName(deptName);
                }
                return new PageUtils(page);
            }
            IPage<ExamScoreEntity> page = this.page(
                    new Query<ExamScoreEntity>().getPage(params),
                    new QueryWrapper<ExamScoreEntity>()
            );
            for(ExamScoreEntity examScoreOne:page.getRecords()){
                SysUserEntity userOne = sysUserService.getById(examScoreOne.getUserId());
                String realName = userOne.getRealname();
                String deptName = sysDeptService.getById((userOne.getDeptId())).getName();
                examScoreOne.setRealName(realName);
                examScoreOne.setDeptName(deptName);
            }
            return new PageUtils(page);
        }
        if(!StringUtils.isNullOrEmpty(usernameQ)){
            QueryWrapper qwu = new QueryWrapper();
            qwu.like("realname",usernameQ);
            List<SysUserEntity> userEntityList = sysUserService.list(qwu);
            for(SysUserEntity users:userEntityList){
                userIdList.add(users.getUserId());
            }
        }
        if(!StringUtils.isNullOrEmpty(deptNameQ)){
            QueryWrapper qwd = new QueryWrapper();
            qwd.eq("name",deptNameQ);
            Long deptId = sysDeptDao.selectOne(qwd).getDeptId();
            QueryWrapper qwus = new QueryWrapper();
            qwus.eq("dept_id",deptId);
            List<SysUserEntity> userEntityLists = sysUserService.list(qwus);
            for(SysUserEntity users:userEntityLists){
                userIdList.add(users.getUserId());
            }
        }
        if(userIdList.size() != 0 ){
            IPage<ExamScoreEntity> page = this.page(
                    new Query<ExamScoreEntity>().getPage(params),
                    new QueryWrapper<ExamScoreEntity>().in("user_id",userIdList)
            );
            for(ExamScoreEntity examScoreOne:page.getRecords()){
                SysUserEntity userOne = sysUserService.getById(examScoreOne.getUserId());
                String realName = userOne.getRealname();
                String deptName = sysDeptService.getById((userOne.getDeptId())).getName();
                examScoreOne.setRealName(realName);
                examScoreOne.setDeptName(deptName);
            }
            return new PageUtils(page);
        }
        IPage<ExamScoreEntity> page = this.page(
                new Query<ExamScoreEntity>().getPage(params),
                new QueryWrapper<ExamScoreEntity>()
        );
        for(ExamScoreEntity examScoreOne:page.getRecords()){
            SysUserEntity userOne = sysUserService.getById(examScoreOne.getUserId());
            String realName = userOne.getRealname();
            String deptName = sysDeptService.getById((userOne.getDeptId())).getName();
            examScoreOne.setRealName(realName);
            examScoreOne.setDeptName(deptName);
        }
        return new PageUtils(page);
    }

    @Override
    public PageUtils queryMyPage(Map<String, Object> params, Long userId) {
        IPage<ExamScoreEntity> page = this.page(
                new Query<ExamScoreEntity>().getPage(params),
                new QueryWrapper<ExamScoreEntity>().eq("user_Id",userId)
        );

        return new PageUtils(page);
    }

}
